package ru.housewife.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import ru.housewife.AppConstants;
import ru.housewife.cache.CacheManager;
import ru.housewife.form.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Controller to work with ingredients
 *
 * @author atroshanin
 */
@Controller
public class IngredientsController extends AuthorizedController implements AppConstants {

    @RequestMapping(value = "/get-select-ingredients-form.html")
    public ModelAndView getSelectIngredientForm() {
        List<IngredientGroupForm> ingredientsGroups = CacheManager.getInstance().getFirstLevelIngredientsGroups();
        return new ModelAndView("housewifes/ingredients/selectIngredientDialog-json-reply", "ingredientsGroups", ingredientsGroups);
    }

    @RequestMapping(value = "/get-ingredients-list.html")
    public ModelAndView getGetIngredients(long groupId,
                                    @RequestParam(required = false) int page,
                                    HttpServletRequest request) {
        UserForm user = getUserForm(request);
        IngredientGroupForm group = CacheManager.getInstance().getIngredientGroupForm(groupId);
        List<IngredientForm> ingredientsList;
        int pageCount;
        if (group != null) {
            int origSize = group.getIngredients().size();
            if (origSize > 0) {
                int from = SELECT_INGREDIENTS_VIEW_PAGE_SIZE * page;
                from = (from >= origSize) ? 0 : from;
                int to = from + SELECT_INGREDIENTS_VIEW_PAGE_SIZE;
                to = (to >= origSize) ? origSize : to;

                ingredientsList = group.getIngredients().subList(from, to);
            } else {
                ingredientsList = Collections.emptyList();
            }
            pageCount = getPageCount(SELECT_INGREDIENTS_VIEW_PAGE_SIZE, origSize);
        } else {
            ingredientsList = Collections.emptyList();
            pageCount = 0;
            page = 0;
        }
        List<WareFormView> defWaresViews = new ArrayList<WareFormView>(ingredientsList.size());
        Map<Long, WareFormView> waresViewsToFillPrices = new HashMap<Long, WareFormView>(ingredientsList.size());
        for (IngredientForm ingredient : ingredientsList) {
            WareForm defWare = ingredient.getDefaultWare();
            WareFormView defWareView = new WareFormView(defWare);
            defWaresViews.add(defWareView);
            UserForm.UsersWare usersWare = user.getUsersWare(defWare.getId());
            if (usersWare != null) {
                defWareView.setRegionPrice(usersWare.getWareRegionPrice());
                defWareView.setUserPrice(usersWare.getWareUserPrice());
            } else {
                waresViewsToFillPrices.put(defWare.getId(), defWareView);
            }
        }
        if (waresViewsToFillPrices.size() > 0) {
            CacheManager.getInstance().fillWaresViewsWithRegionPrices(user.getRegion(), waresViewsToFillPrices);
        }

        ModelAndView mv = new ModelAndView("housewifes/ingredients/ingredientsList-json-reply");
        mv.addObject("ingredients", ingredientsList);
        mv.addObject("defWaresViews", defWaresViews);
        mv.addObject("selectedPage", page);
        mv.addObject("pageCount", pageCount);
        mv.addObject("pageNums", generatePageNumsInfo(page, pageCount));
        return mv;
    }

}
